import {router,addRoutes} from "~/router";
import { getToken } from "~/composables/auth"
import { 
	toast,
	showFullLoading,
	hideFullLoading ,
	} from "~/composables/util"
import store from "./store";
import { Console } from "windicss/utils";

let hasGetInfo = false
router.beforeEach(async(to,from,next)=>{
	showFullLoading()
	
	const token = getToken()
	if(!token && to.path !="/login"){
		toast("请先登录","error")
		
		return next({path:"/login"})
	}
	
	if(token && to.path == "/login"){
		toast("请勿重复登录","error")
		return next({ path:from.path ? from.path : "/"})
	}
	let hasNewRoutes=false
	if(token && !hasGetInfo){
		let {menus}= await store.dispatch("getinfo")
		
		hasNewRoutes=addRoutes(menus)
	}
	let title = (to.meta.title ? to.meta.title : "") + "飞天商城后台"
	document.title = title
	hasNewRoutes ? next(to.fullPath) :next()
})

router.afterEach((to,from)=>hideFullLoading())